package com.lzb.musicplayer.activity;

import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.basic.utils.MediaUtil;
import com.lzb.musicplayer.BaseAc;
import com.lzb.musicplayer.BasePlayerActivity;
import com.lzb.musicplayer.Content;
import com.lzb.musicplayer.R;
import com.lzb.musicplayer.adapter.RingtoneAdapter;
import com.lzb.musicplayer.bean.MusicFile;
import com.lzb.musicplayer.databinding.ActivityMyRingtoneBinding;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MyRingtoneActivity extends BasePlayerActivity<ActivityMyRingtoneBinding> implements View.OnClickListener, OnItemClickListener {

    private RingtoneAdapter ringtoneAdapter;
    private List<MusicFile> listShow = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_my_ringtone;
    }

    @Override
    protected void initView() {
        mDataBinding.ivMyRingtoneBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.llPlay.setOnClickListener(this);
        mDataBinding.llRandomPlay.setOnClickListener(this);

        mDataBinding.rvAudioList.setLayoutManager(new LinearLayoutManager(mContext));
        ringtoneAdapter = new RingtoneAdapter();
        mDataBinding.rvAudioList.setAdapter(ringtoneAdapter);
        ringtoneAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.llPlay) {//播放全部
            setFragmentMusicList(listShow);
        } else if (id == R.id.llPlay) {//随机播放
            if (listShow.size() == 0) return;
            mDataBinding.audioPlayerView.setSj();
            mDataBinding.audioPlayerView.startPlay();
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        mDataBinding.audioPlayerView.setPlayList(listShow, position);
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        listShow.clear();
        String path = PathUtils.getFilesPathExternalFirst() + Content.myRingtone;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                MusicFile musicFile = new MusicFile();
                musicFile.setTitle(FileUtils.getFileName(file));
                musicFile.setPath(file.getPath());
                musicFile.setDuration(MediaUtil.getDuration(file.getPath()));
                listShow.add(musicFile);
            }
        }
        if (listShow.size() > 0) {
            mDataBinding.audioPlayerView.setPlayList(listShow, 0);
            ringtoneAdapter.setList(listShow);
            mDataBinding.rvAudioList.setVisibility(View.VISIBLE);
            mDataBinding.tvAudioNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvAudioList.setVisibility(View.GONE);
            mDataBinding.tvAudioNoData.setVisibility(View.VISIBLE);
        }
    }

    private void setFragmentMusicList(List<MusicFile> listFile) {
        if (listFile != null && !listFile.isEmpty()) {
            mDataBinding.audioPlayerView.setPlayList(listFile, 0);
            mDataBinding.audioPlayerView.startPlay();
        }
    }

    @Override
    public void onPlaybackStateChanged(boolean isPlaying) {
        if (isPlaying) {
            mDataBinding.audioPlayerView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onPlaybackStateChanged(int state) {

    }

    @Override
    public void onProgressUpdated(long position, long duration) {

    }

    @Override
    public void onTrackChanged(MusicFile currentTrack) {

    }
}